--中央区显示相关
local CenterArea = fk.CreateSkill{
  name = "#CenterArea",
}

Fk:loadTranslationTable{
  ["@$CenterArea"] = "中央区",
  ["$CenterArea"] = "中央区",
}

local DIY = require "packages/diy_utility/diy_utility"

CenterArea:addEffect(fk.AfterCardsMove, {
  priority = 0,
  can_refresh = Util.TrueFunc,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.discard_pile, function (id)
      if Fk:getCardById(id):getMark(MarkEnum.IncludeCenterCards) > 0 then
        return true
      end
      for _, suffix in ipairs(MarkEnum.TempMarkSuffix) do
        if Fk:getCardById(id):getMark(MarkEnum.IncludeCenterCards..suffix) > 0 then
          return true
        end
      end
      return false
    end)
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end
    end, Player.HistoryTurn)
    cards = table.filter(cards, function (id)
      local card = Fk:getCardById(id)
      return table.contains(room.discard_pile, id) and
        table.every(MarkEnum.TempMarkSuffix, function (suffix)
          return card:getMark(MarkEnum.RemoveCenterCards..suffix) == 0
        end)
    end)
    DIY.setCenterCards(room, cards)
  end
})

CenterArea:addEffect(fk.AfterDrawPileShuffle, {
  priority = 0,
  can_refresh = Util.TrueFunc,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.discard_pile, function (id)
      if Fk:getCardById(id):getMark(MarkEnum.IncludeCenterCards) > 0 then
        return true
      end
      for _, suffix in ipairs(MarkEnum.TempMarkSuffix) do
        if Fk:getCardById(id):getMark(MarkEnum.IncludeCenterCards..suffix) > 0 then
          return true
        end
      end
      return false
    end)
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end
    end, Player.HistoryTurn)
    cards = table.filter(cards, function (id)
      local card = Fk:getCardById(id)
      return table.contains(room.discard_pile, id) and
        table.every(MarkEnum.TempMarkSuffix, function (suffix)
          return card:getMark(MarkEnum.RemoveCenterCards..suffix) == 0
        end)
    end)
    local orig_ids = DIY.getCenterCards(room)
    local ids = table.filter(orig_ids, function (id)
      return not table.contains(cards, id)
    end)
    if #ids > 0 then
      room.logic:trigger(DIY.BeforeCenterAreaRemove, nil, {ids = ids})
    end
    DIY.setCenterCards(room, cards)
    if #ids > 0 then
      room.logic:trigger(DIY.AfterCenterAreaRemove, nil, {ids = ids})
    end
  end,
})

CenterArea:addEffect(fk.PreTurnStart, {
  priority = 0,
  can_refresh = function (self, event, target, player, data)
    return player == target
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local ids = DIY.getCenterCards(room)
    if #ids > 0 then
      room.logic:trigger(DIY.BeforeCenterAreaRemove, nil, {ids = ids})
    end
    DIY.setCenterCards(room, {})
    if #ids > 0 then
      room.logic:trigger(DIY.AfterCenterAreaRemove, nil, {ids = ids})
    end
  end
})

return CenterArea